// 1. 计算一致字符串
function fn1(allowed: string, words: string[]): number {
    var n: number = 0
    words.forEach((item) => {
        const narr = item.split('')
        if (narr.every((item) => allowed.indexOf(item) !== -1)) {
            n++
        }
    })
    return n
}
console.log(fn1('abc', ['ab', 'ac', 'bc', 'abc', 'acd', 'aa']));


// 2. 获取url及封装深拷贝方法
function fn2(str2: string) {
    var newStr: any[] = []
    newStr.push.apply(newStr, str2.split(''))
    for (var i = 0; i < newStr.length; i++) {
        if (typeof (Number(newStr[i]))) {
            console.log(newStr[i]);
        }
    }
}
// 3. 买卖股票的最佳时机
function gup(prices: number[]) {
    let pro = 0
    for (var i = 0; i < prices.length; i++) {
        let tmp = prices[i] - prices[i - 1]
        if (tmp > 0) {
            pro += tmp
        }
    }
    return pro
}
console.log(gup([6, 2, 4, 3, 5, 1]));

// 4. 找只出现一次的元素
function fnc(nums: number[]) {
    let num = -1
    for (var i = 0; i < nums.length; i++) {
        if (nums.indexOf(nums[i]) == nums.lastIndexOf(nums[i])) {
            num = nums[i]
        }
    }
    return num
}
console.log(fnc([1, 2, 3, 2, 1, 4, 5]));

// 5. 多维转一纬
function flat<T>(arr: Array<T>, k = 1): Array<T> {
    let narr: Array<T> = []
    arr.forEach((item: T) => {
        if (Array.isArray(item) && k >= 1) {
            narr = narr.concat(flat(item, k - 1))
        } else {
            narr = narr.concat(item)
        }
    })
    return narr
}
console.log(flat([1, [1, 2, 3, [1, 2, 3, 4, 5, 6]]]));
